<template>
  <div class="employees-container">
    <div class="app-container">
      <!-- 通用工具栏组件使用/头部区域 -->
      <page-tools>
        <!-- 自定义左侧内容 -->
        <template #slot-left>
          <span>共{{ total }}条记录</span>
        </template>

        <!-- 自定义右侧内容 -->
        <template #slot-right>
          <el-button v-power="'import'" type="danger" size="small" @click="$router.push('/excel')">导入excel</el-button>
          <el-button v-power="'export'" type="success" size="small" @click="handleDownload">导出excel</el-button>
          <el-button type="primary" size="small" @click="addshowdialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <!-- 主体区域 -->
      <el-card style="margin-top: 10px">
        <el-table border :data="employeesList" :default-sort="{prop: 'workNumber'}">
          <el-table-column type="index" label="序号" />
          <el-table-column prop="username" label="姓名" />
          <el-table-column label="头像">
            <template slot-scope="scope">
              <image-holder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>

          <el-table-column prop="mobile" label="手机号" />
          <el-table-column prop="workNumber" label="工号" />
          <el-table-column prop="formOfEmployment" label="聘用形式" :formatter="formatter" />
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column sortable label="入职时间">
            <template slot-scope="scope">
              <span>{{ Time( scope.row.timeOfEntry) }}</span>
            </template>
          </el-table-column>
          <el-table-column sortable label="转正时间">
            <template slot-scope="scope">
              <span>{{ Time( scope.row.correctionTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push('/employees/detail?id=' + scope.row.id)">查看</el-button>
              <el-button type="text" size="small" @click="RoleEmp(scope.row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmp(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页区域 -->
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25]"
            :page-size="query.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          /> </el-row>

        <!-- 新增员工弹出层 -->
        <el-dialog
          v-if="addshowdialog"
          title="提示"
          :visible.sync="addshowdialog"
          width="50%"
        >
          <empDialog @update-empolyee="updateEmpolyee" @close="addshowdialog=false" />
        </el-dialog>
        <!-- 分配角色弹出层 -->
        <el-dialog
          v-if="Rolesdialog"
          title="提示"
          :visible.sync="Rolesdialog"
          width="50%"
        >
          <AssignRole :user-roleid="userRoleid" @close="closeRoleinfo" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
// 引入时间处理插件
import { Time } from '@/utils/date'
// 引入接口
import { getEmployeeList, delEmployee } from '@/api/employees'
// 引入弹出层里面的插件
import empDialog from './empDialog.vue'
// 引入枚举判断是否是正式员工
import EmployeeEnum from '@/constant/employees'
// 引入分配权限组件
import AssignRole from './assignRole'
export default {
  name: 'Employees',
  components: {
    empDialog,
    AssignRole
  },
  data() {
    return {
      addshowdialog: false, // 弹出层显示隐藏
      query: {
        page: 1, // 页码
        size: 10 // 每页条数
      },
      employeesList: [], // 员工列表
      total: 0, // 数据总条数,
      Rolesdialog: false, // 分配权限弹出层,显示或隐藏
      userRoleid: ''
    }
  },
  created() {
    // 调用获取员工信息的接口
    this.getEmployeeList()
  },
  methods: {
    // 点击分配角色按钮
    RoleEmp(id) {
      this.userRoleid = id
      this.Rolesdialog = true
    },
    transData(rows) {
      // 第一步:枚举,目的是为了处理后台返回的数据
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      // 第二步: 先获取所有的表头元素
      const headerKeys = Object.keys(rows[0])
      // 第三部 : 把所有的表头 ,用枚举的方式 ,转换成汉字的 ,放在一个变量里面,这样表头就弄好了
      const Header = headerKeys.map(item => {
        return map[item]
      })
      const data = rows.map(obj => {
        return Object.values(obj)
      })
      return { Header, data }
    },
    // 点击导出按钮
    async handleDownload() {
      // 获取员工信息列表
      const res = await getEmployeeList(this.query)
      //   转换数据格式 , 转换好以后交给Excel处理插件

      const excelObj = this.transData(res.data.rows)
      //   Excel导出插件
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header: excelObj.Header,
          data: excelObj.data,
          filename: '员工列表',
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
    },
    Time,
    updateEmpolyee() {
      this.addshowdialog = false
      this.getEmployeeList()
    },
    // 获取员工信息的接口
    async getEmployeeList() {
      const res = await getEmployeeList(this.query)
      this.employeesList = res.data.rows
      this.total = res.data.total
    },
    // 每页显示的条数发生改变时触发
    handleSizeChange(newSize) {
      this.query.size = newSize
      this.getEmployeeList()
    },

    // 当前页面发生改变时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getEmployeeList()
    },
    // formatter方法 ,引入枚举判断是否是正式员工
    formatter(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    // 删除员工
    async delEmp(id) {
      const delRes = await this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (delRes === 'cancel') {
        return this.$message('已经取消删除')
      }
      const res = await delEmployee(id)
      this.$message.success(res.message)
      if (this.employeesList.length === 1) {
        this.query.page--
        if (this.query.page <= 0) {
          this.query.page = 1
        }
      }
      this.getEmployeeList()
    },
    // 关闭员工分配权限弹窗
    closeRoleinfo() {
      this.Rolesdialog = false
    }

  }
}
</script>

<style lang="scss" scoped>
.imgpro{
    width: 100px;
    height: 100px;
}
</style>
